package xhwl.hmo.mall.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

import xhwl.hmo.mall.BaseActivity;
import xhwl.hmo.mall.R;
import xhwl.hmo.mall.http.api.AddressInfoApi;
import xhwl.hmo.mall.http.bean.resp.AddressInfoResp;
import xhwl.hmo.mall.http.bean.resp.HttpResp;
import xhwl.hmo.mall.utils.XHLog;
import xhwl.retrofitrx.HttpManager;
import xhwl.retrofitrx.OnNextListener;

/**
 * Created by admin on 2017-10-30.
 */

public class SelectAddrActivity extends BaseActivity {
    private static final int PROVINCE=0;
    private static final int CITY=1;
    private static final int AREA=2;
    ListView listView;
    TextView tvPointer;
    AddressInfoResp.ProvinceInfo curProvince;
    AddressInfoResp.CityInfo curCity;
    ArrayList<AddressInfoResp.ProvinceInfo> provinceList=new ArrayList<>();
    ArrayList<AddressInfoResp.CityInfo> cityList=new ArrayList<>();
    ArrayList<AddressInfoResp.DistrictInfo> areaList=new ArrayList<>();
    AddrAdapter adapter;
    int current =PROVINCE;//当前省、市、区
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_addr);
    }

    @Override
    public void initViews() {
        initTitleViews();
        tvPointer= (TextView) findViewById(R.id.tvPointer);
        listView= (ListView) findViewById(R.id.listView);
        adapter=new AddrAdapter();

    }

    @Override
    protected void setUpTitleView() {
        super.setUpTitleView();
        this.titleTextView.setText("选择地区");
    }

    @Override
    public void onBackPressed() {
        if(current ==PROVINCE){
            super.onBackPressed();
        }
        else if(current ==CITY){
            current =PROVINCE;
            curCity=null;
            tvPointer.setVisibility(View.GONE);
            tvPointer.setText("");
            listView.setAdapter(adapter);
        }else if(current ==AREA){
            current =CITY;
            tvPointer.setVisibility(View.VISIBLE);
            tvPointer.setText(curProvince.PROVINCE_NAME);
            listView.setAdapter(adapter);
        }
    }

    @Override
    public void initListener() {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(current ==PROVINCE){
                    curProvince=provinceList.get(position);
                    loadData(curProvince.PROVINCE_ID,"0","0");
                }
                else if(current ==CITY){
                    curCity=cityList.get(position);
                    loadData(curProvince.PROVINCE_ID,curCity.CITY_ID,"0");
                }else if(current ==AREA){
                    Intent intent=new Intent();
                    intent.putExtra("province",curProvince);
                    intent.putExtra("city",curCity);
                    intent.putExtra("area",areaList.get(position));
                    XHLog.e(curProvince.PROVINCE_NAME+curCity.CITY_NAME+areaList.get(position).DISTRICT_NAME);
                    setResult(RESULT_OK,intent);
                    finish();
                }
            }
        });
    }

    @Override
    public void loadData() {
        loadData("0","0","0");
    }
    private void loadData(final String pro_id, final String city_id, final String area_id){
        AddressInfoApi api=new AddressInfoApi(pro_id,city_id,area_id);
        HttpManager.getInstance().dealHttp(this, api, new OnNextListener<HttpResp<AddressInfoResp>>() {
            @Override
            public void onNext(HttpResp<AddressInfoResp> resp) {
                if("0".equals(pro_id)){//列出所有省
                    provinceList.clear();
                    provinceList=resp.data.province_info;
                    XHLog.e("sheng="+provinceList.size()+"");
                    current =PROVINCE;
                }
                else if(!"0".equals(pro_id)&&"0".equals(city_id)){//列出所有市
                    cityList.clear();
                    cityList=resp.data.city_info;
                    current =CITY;
                    tvPointer.setVisibility(View.VISIBLE);
                    tvPointer.setText(curProvince.PROVINCE_NAME);
                }
                else if(!"0".equals(pro_id)&&!"0".equals(city_id)&&"0".equals(area_id)){//列出所有区
                    areaList.clear();
                    areaList=resp.data.district_info;
                    current =AREA;
                    tvPointer.setVisibility(View.VISIBLE);
                    tvPointer.setText(curProvince.PROVINCE_NAME+" "+curCity.CITY_NAME);
                }
                listView.setAdapter(adapter);
            }
        });
    }
    class AddrAdapter extends android.widget.BaseAdapter{
        @Override
        public int getCount() {
            int count=0;
            if(current ==PROVINCE){
                count= provinceList.size();
            }
            else if(current ==CITY){
                count= cityList.size();
            }else if(current ==AREA){
                count= areaList.size();
            }
            XHLog.d("count="+ count);
            return count;
        }

        @Override
        public Object getItem(int position) {
            if(current ==PROVINCE){
                return provinceList.get(position);
            }
            else if(current ==CITY){
                return cityList.get(position);
            }else if(current ==AREA){
                return areaList.get(position);
            }
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder=null;
            if(convertView==null){
                holder=new ViewHolder();
                convertView= LayoutInflater.from(mContext).inflate(R.layout.addr_item,null);
                holder.tvAddrName= (TextView) convertView.findViewById(R.id.tvAddrName);
                convertView.setTag(holder);
            }
            holder= (ViewHolder) convertView.getTag();
            String name="";
            if(current ==PROVINCE){
                name=provinceList.get(position).PROVINCE_NAME;
            }
            else if(current ==CITY){
                name=cityList.get(position).CITY_NAME;
            }else if(current ==AREA){
                name=areaList.get(position).DISTRICT_NAME;
            }
            holder.tvAddrName.setText(name);
            return convertView;
        }
        class ViewHolder{
            TextView tvAddrName;
        }
    }
}
